package com.example.human_resource_management.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.human_resource_management.common.ResponseData;
import com.example.human_resource_management.config.UserContextUtils;
import com.example.human_resource_management.entry.dto.ResourceApplyDto;
import com.example.human_resource_management.entry.po.ResourceApply;
import com.example.human_resource_management.mapper.ResourceApplyMapper;
import com.example.human_resource_management.service.ResourceApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ResourceApplyServiceImpl extends ServiceImpl<ResourceApplyMapper, ResourceApply>
        implements ResourceApplyService{

    @Autowired
    private ResourceApplyMapper resourceApplyMapper;

    @Override
    public List<ResourceApplyDto> selectResourceAppListByEmpId(Long empId) {
        return resourceApplyMapper.selectResourceAppListByEmpId(empId);
    }

    @Override
    public ResponseData<String> createResourceApply(Long resId) {
        Long empId = UserContextUtils.getEmpId();
        if (empId == null) {
            return ResponseData.warn("未获取到员工信息");
        }
        ResourceApply apply = new ResourceApply();
        apply.setEmpId(empId);
        apply.setResId(resId);
        int result = resourceApplyMapper.insert(apply);
        if (result > 0) {
            return ResponseData.ok("资源申请提交成功");
        } else {
            return ResponseData.warn("资源申请提交失败");
        }
    }

    @Override
    public ResponseData<String> cancelApply(Long applyId) {
        Long empId = UserContextUtils.getEmpId();
        if (empId == null) {
            return ResponseData.warn("未获取到员工信息，无法撤回申请");
        }
        String newStatus = "已撤回";
        int affectedRows = getBaseMapper().cancelApply(applyId, empId, newStatus);
        if (affectedRows == 1) {
            return ResponseData.ok("申请已成功撤回");
        } else {
            return ResponseData.warn("撤回失败！可能原因：申请不存在、状态不是待审，或您无权限撤回");
        }
    }

    @Override
    public List<ResourceApplyDto> selectAllResourceAppList() {
        return resourceApplyMapper.selectAllResourceAppList();
    }

    @Override
    public ResponseData<String> insertResourceApply(ResourceApply resourceApply) {
        int result = resourceApplyMapper.insertResourceApply(resourceApply);
        if (result > 0) {
            return ResponseData.ok("资源申请添加成功");
        } else {
            return ResponseData.warn("资源申请添加失败");
        }
    }

    @Override
    public ResponseData<String> updateResourceApply(ResourceApply resourceApply) {
        int result = resourceApplyMapper.updateResourceApply(resourceApply);
        if (result > 0) {
            return ResponseData.ok("资源申请更新成功");
        } else {
            return ResponseData.warn("资源申请更新失败");
        }
    }

    @Override
    public ResponseData<String> deleteResourceApply(Long applyId) {
        int result = resourceApplyMapper.deleteResourceApply(applyId);
        if (result > 0) {
            return ResponseData.ok("资源申请删除成功");
        } else {
            return ResponseData.warn("资源申请删除失败");
        }
    }
}